/*----------------------------------------------------------------------------
Owner(s): Shane Whitfield
$Copyright: $
$Id: $
----------------------------------------------------------------------------*/

#include "model.h"
#include "render.h"
#include "go.h"
#include "texture.h"

/*--------------------------------------------------------------------------*/
CModel::CModel(const CGo* i_owningGo)
{
	assert(i_owningGo);

	m_owningGo = i_owningGo;
	SetBoundingRadius(0.2f);
	
	m_texture = NULL;
}

void CModel::SetTexture(const char* i_textureName)
{
	m_texture = g_texturemgr.CreateTexture(i_textureName);
}

void CModel::Update()
{
	//DO NOTHING for now.
}

bool CModel::IsPointWithin(const Vector2f& i_pos)
{
    return Dist(i_pos, m_owningGo->GetPos()) <= m_boundingRadius;
}

void CModel::Render()
{
	if (m_texture)
	{
		//create the quad from the bounding radius and pos.
		//$$$$ this is a fast semi-approximation of the square created by a circle.. but by no means correct
		float xMin = -GetBoundingRadius();
		float xMax = GetBoundingRadius();
		float yMin = -GetBoundingRadius();
		float yMax = GetBoundingRadius();
		
        // BB (TEF) is this efficient? 
        if (m_color.a > 0.5)
            Renderer::SetAlphaFunc(Renderer::EBarrier, 0.5);
        else
            Renderer::SetAlphaFunc(Renderer::EFull);
        
        g_texturemgr.EnableTextures(true);
		m_texture->Apply();
    
		m_owningGo->GetTransform().PushMatrix();
		BasicDrawObjects::DrawTexturedRect(Vector2f(xMin, yMin), Vector2f(xMax, yMax), m_color, Vector2f(0.0f, 0.0f), Vector2f(1.0f, 1.00f));
		m_owningGo->GetTransform().PopMatrix();
		
		g_texturemgr.EnableTextures(false);
        
        Renderer::SetAlphaFunc(Renderer::ENone);
       
	}
	else
	{
		Vector2f unused;
		BasicDrawObjects::DrawSolidCircle(m_owningGo->GetPos(), GetBoundingRadius(), unused, GetColor());
	}
	
}

/*-----------------------------------EOF!-----------------------------------*/ 
